<?php
	session_start();
	
	require_once('../../lib/'.'BaseDir.inc.htm');
	require_once (EIU_LIB.'View.class.htm');
	require_once(EIU_LIB.'DBCommon.class.htm');
	
	if(isset($_POST['page']) && !empty($_POST['page'])){
		$startLimit = ($_POST['page']-1)*10;
		$endLimit = 10;
	}
	
	$fromDate = $_POST["fromDate"];
	$toDate = $_POST["toDate"];
	
	if($fromDate == "") $fromDate = "1970-01-01";
	if($toDate == "") $toDate = date("Y-m-d");

	$condition = "`CostName` LIKE '%".$_POST['costName']."%' AND CreateDate >= '".$fromDate."' AND CreateDate <= '".$toDate."'";
	
	$arr = "";

	$count = "SELECT COUNT(ID) as Count FROM `cost_tbl` WHERE `isDeleted`=0 AND ".$condition;
	$q = "
		SELECT 
			 `ID`,`CostName`,`Amount`,`CreateDate`,`LastUpdate`,`ModifiedBy`,`Remark`
		FROM `cost_tbl` 
		WHERE `isDeleted`=0 AND ".$condition." 
		ORDER BY `CostName` 
		LIMIT ".$startLimit.",".$endLimit;
		
	$oDB = new DBCommon();
	$conn =  $oDB->stl_openConn();
	
	$rs = $oDB->stl_db_select($q, $conn);
	$rs_count = $oDB->stl_db_select($count, $conn);
	if(!empty($rs_count[0]->count)){
		$arr["Count"] = $rs_count[0]->count;
	}
	
	$i = 0;
	if(is_array($rs)){
		foreach($rs as $key=>$value){
			$data1['ID'] = $rs[$key]->ID;
			$data1['CostName'] = $rs[$key]->CostName;
			$data1['Amount'] = $rs[$key]->Amount;
			$data1['CreateDate'] = $rs[$key]->CreateDate;
			$data1['LastUpdate'] = $rs[$key]->LastUpdate;
			$data1['ModifiedBy'] = $rs[$key]->ModifiedBy;
			$data1['Remark'] = $rs[$key]->Remark;
			
			
			$arr["Result"][$i] = $data1;
			$i++;
		}
		echo json_encode($arr);
	}else{
		echo 'NG';
	}
	
	$oDB->stl_closeConn($conn);
	

?>
